package SimulationImplementation;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> arrayList = new ArrayList<>();
    Account atmTemp = new Account();

    Scanner scanner = new Scanner(System.in);
    Random random = new Random();
    public void start() {
        while (true) {
            System.out.println("==========欢迎进入银行ATM系统==========");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("3.退出系统");
            System.out.println("请你选择你要操作的命令");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    //用户登录
                    login();

                    break;
                case 2:
                    //用户开户
                    createAccount();
                    break;
                case 3:
                    System.out.println("已退出ATM系统");
                    return;
                default:
                    System.out.println("输入有误，请重新输入：");
                    break;
            }
        }
    }
    private void login(){
        if (arrayList.size()==0){
            System.out.println("还没有账号不允许登录，先去开户~");
            return;
        }

        while (true){
            System.out.println("先输入卡号：");
            String cardNumber = scanner.next();
            if (judgementGetCardNumber(cardNumber)==null){
                System.out.println("卡号不存在，请重新输入：");

            }else {
                int i = 0;
                for ( i = 3; i > 0; i--) {
                    System.out.println("请输入账户密码：");
                    String password = scanner.next();
                    if (atmTemp.getPassword().equals(password)){
                        System.out.println(atmTemp.getName() +"账户登录成功");
                        showUserCommand();
                        return;
                    }else {
                        System.out.println("密码输入错误："+"还有"+(i-1)+"次机会");
                    }
                }
                if (i==0){
                    System.out.println("机会用完了，退出系统，拜拜！");
                    return;
                }
            }
        }
    }

    private void showUserCommand(){
        while (true){
            System.out.println(atmTemp.getName()+"您可以选择一下功能进行账户的处理：");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.密码修改");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            System.out.println("请选择：");
            int command = scanner.nextInt();
            switch (command){
                case 1:
                    findAccount();
                    break;
                case 2:
                    savesMoney();
                    break;
                case 3:
                    withdrawMoney();
                    break;
                case 4:
                    transferMoney();
                    break;
                case 5:
                    changePassword();
                    break;
                case 6:
                    System.out.println("退出对账号的处理:");
                    return;
                case 7:
                    cancelAccount();
                    return;
                default:
                    System.out.println("输入有误，请重新输入：");
            }

        }
    }
    //注销账户
    private void cancelAccount(){
        if (atmTemp.getBalance()!=0){
            System.out.println("账户还有余额，不能注销账户");
            return;
        }else {
            arrayList.remove(atmTemp);
            System.out.println("注销成功");
            return;
        }
    }

    //修改密码
    private void changePassword(){
        System.out.println("确认要修改密码吗？y/n");
        char ch = scanner.next().charAt(0);
        if (ch == 'n'){
            System.out.println("取消修改密码：");
        }else if (ch == 'y'){
            System.out.println("请输入要修改的密码：");
            String pass = scanner.next();
            atmTemp.setPassword(pass);
        }else {
            System.out.println("输入错误：");
        }
    }

    //转账
    private void transferMoney(){
        Account transferredMoney = new Account();
        transferredMoney = atmTemp;
        if (arrayList.size()<2){
            System.out.println("只有一个账户，不能进行操作转账：");
        }
        while (true){
            System.out.println("请输入要转账的账户卡号");
            String cardNumber = scanner.next();
            if (judgementGetCardNumber(cardNumber)==null){
                System.out.println("找不到该卡号，请重新输入");
            }else {
                Account acceptMoney = new Account();
                acceptMoney = atmTemp;
                System.out.println("请您输入需要转账的数目：");
                double money = scanner.nextDouble();
                if (money > acceptMoney.getLimit()){
                    System.out.println("转入的账号限额了");
                    atmTemp = transferredMoney;
                    return;
                }else if(money > transferredMoney.getLimit()) {
                    System.out.println("转出的账号限额了");
                    atmTemp = transferredMoney;
                    return;
                }
                if(money > transferredMoney.getBalance()){
                    System.out.println("余额不足，转帐失败");
                    atmTemp = transferredMoney;
                    return;
                }else {
                    transferredMoney.setBalance(transferredMoney.getBalance()-money);
                    System.out.println("转帐成功，余额还剩"+transferredMoney.getBalance());
                    acceptMoney.setBalance(acceptMoney.getBalance()+money);
                    System.out.println(acceptMoney.getName()+"成功转入"+"余额为："+acceptMoney.getBalance());
                    atmTemp = transferredMoney;
                    return;
                }

            }
        }

    }

    //取钱
    private void withdrawMoney(){
        System.out.println("请输入需要取钱数目");
        double money = scanner.nextDouble();
        if (money > atmTemp.getLimit()){
            System.out.println("限额了");
            return;
        }
        if(money > atmTemp.getBalance()){
            System.out.println("余额不足，取钱失败");
        }else {
            atmTemp.setBalance(atmTemp.getBalance()-money);
            System.out.println("取钱成功，余额还剩"+atmTemp.getBalance());
        }
    }



    //存钱
    private void savesMoney(){
        while (true){
            System.out.println("请输入存款的数目：");
            double money = scanner.nextDouble();
            if (money > atmTemp.getLimit()){
                System.out.println("限额了");
                return;
            }
            if (money>0){
                atmTemp.setBalance(money);
                System.out.println("存钱成功");
                break;
            }else {
                System.out.println("输入不符合规定，重新输入：");
            }
        }

    }

    //查询账户
    private void findAccount(){
        System.out.println("==========当前账户如下：============");
        System.out.println("卡号："+ atmTemp.getCardNumber());
        System.out.println("户主："+ atmTemp.getName());
        System.out.println("性别："+ atmTemp.getSex());
        System.out.println("余额："+ atmTemp.getBalance());
        System.out.println("限额："+ atmTemp.getLimit());
    }


    private void createAccount(){
        Account account = new Account();

        System.out.println("请输入名字：");
        String name = scanner.next();
        account.setName(name);

        while (true){
            System.out.println("请输入性别:");
            char sex = scanner.next().charAt(0);
            if (sex == '男' || sex == '女'){
                account.setSex(sex);
                break;
            }else {
                System.out.println("不符合规定，请重新输入：");
            }
        }

        while (true){
            System.out.println("请设置密码：");
            String password = scanner.next();
            System.out.println("再次确认密码：");
            String rePassword = scanner.next();
            if (password.equals(rePassword)){
                System.out.println("已确认密码完毕");
                account.setPassword(rePassword);
                break;
            }else {
                System.out.println("两次密码不一致，请重新输入：");
            }
        }

        System.out.println("请输入限额：");
        double limit = scanner.nextDouble();
        account.setLimit(limit);

        //设置卡号且不重复，封装成两个函数：先设置卡号，再判断是否重复
        while (true){
            String cardNumber = setCardNumber();
            if (judgementGetCardNumber(cardNumber) == null){
                account.setCardNumber(cardNumber);
                break;
            }
        }
        System.out.println(account.getName() + "开户成功 " + "您的卡号为：" + account.getCardNumber());

        arrayList.add(account);
    }

    private String setCardNumber(){
        String cardNumber = "";
        for (int i = 0; i < 8; i++) {
            int count = random.nextInt(10);
            cardNumber += count;
        }
        return cardNumber;
    }


    private String judgementGetCardNumber(String cardNumber){
        if (arrayList.size() == 0){
            return null;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getCardNumber().equals(cardNumber)){
                atmTemp = arrayList.get(i);
                return arrayList.get(i).getCardNumber();
            }
        }
        return null;

    }


}
